#include "crvm.h"


byte ram[4][0x20];
romaddr stack[MAXSTACK];
romaddr rom[4][0x200];
byte implicit_registers[8];

byte *ind ;
byte *tcr;
byte *pcl;
byte *sfr;
byte *mir;
byte *rpa;
byte *rpb;
byte *rpc;

byte *p2d;
byte *p3d;
byte *pwc;
byte *adf;
byte *add;
byte *adh;
byte *adc;

byte *wtc;
byte *tcc;
byte *ipa;
byte *opt;
byte *hgr;
byte *cpa;
byte *cpb;
byte *cpc;

romaddr pc;
byte acc;

static byte splevel = 0;


byte read_reg_abs (byte addr)
{
	byte regv;
	if(addr ==0 )
		addr = *mir;

	if((*opt & (1<<7)))
	{
		regv = ram[ (addr & 60 ) >> 5 ][addr &0x1F];
	}
	else 
		regv =*((byte *) ram + addr);

	if(addr == 4)
		regv |= 0x80;

	return regv;
}

void write_reg_abs (byte addr, byte data)
{
	if(addr ==0 )
		addr = *mir & 0x7F;

	if((*opt & (1<<7)))
	{
		ram[(addr & 60 ) >> 5 ][addr &0x1F] = data;
	}
	else 
		*((byte *) ram + addr) = data;

}

void initial_hard(void)
{
	ind = (byte *)&ram[0][0];
	tcr = (byte *)&ram[0][1];
	pcl = (byte *)&ram[0][2];
	sfr = (byte *)&ram[0][3];
	mir = (byte *)&ram[0][4];
	rpa = (byte *)&ram[0][5];
	rpb = (byte *)&ram[0][6];
	rpc = (byte *)&ram[0][7];
	p2d = (byte *)&ram[0][0x69];
	p3d = (byte *)&ram[0][0x6A];
	pwc = (byte *)&ram[0][0x6B];
	adf = (byte *)&ram[0][0x6C];
	add = (byte *)&ram[0][0x6D];
	adh = (byte *)&ram[0][0x6E];
	adc = (byte *)&ram[0][0x6F];

	wtc = implicit_registers+0;
	tcc = implicit_registers+1;
	ipa = implicit_registers+2;
	opt = implicit_registers+3;
	hgr = implicit_registers+4;
	cpa = implicit_registers+5;
	cpb = implicit_registers+6;
	cpc = implicit_registers+7;
}

void push (romaddr addr)
{
	int i;
	stack [splevel++] = addr;
	if(splevel == MAXSTACK)
		printf("stack overload");
}


romaddr pop (void)
{
	romaddr result;
	
	if(splevel == 0)
		printf("stack if empty");
	result = stack [--splevel];

	return result;
}


